package de.imdb250junkie.gui;

import java.awt.Container;
import java.awt.DisplayMode;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

import de.imdb250junkie.db.DB;
import de.imdb250junkie.db.DB.UpdateFailedException;
import de.imdb250junkie.imdb.Top250Reader.MovieNotFoundException;
import de.imdb250junkie.locale.LocaleUtils;

/**
 * @author André Stannek
 * @version 2010-02-10
 */
public class UpdateWindow extends JFrame
{

    /**
     * 
     */
    private static final long serialVersionUID = -756319048453415595L;

    /**
     * Creates a new update dialog and runs the update.
     * 
     * @param database
     *            the Database to update
     * @param listener
     *            listener to inform
     */
    public UpdateWindow(final DB database, final UpdateDoneListener listener)
    {
        super();
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        final Container contentPane = new Container();
        final JProgressBar progress = new JProgressBar();
        progress.setMinimum(0);
        progress.setValue(0);
        progress.setStringPainted(true);
        contentPane.setLayout(new GridLayout(2, 1));
        contentPane.add(new JLabel(ResourceBundle.getBundle(
                "de.imdb250junkie.locale.GUIStrings", LocaleUtils.getLocale())
                .getString("update_running")));
        contentPane.add(progress);

        final DisplayMode displayMode = GraphicsEnvironment
                .getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDisplayMode();
        this.setBounds((displayMode.getWidth() - 300) / 2,
                (displayMode.getHeight() - 100) / 2, 300, 100);
        this.setContentPane(contentPane);
        this.setVisible(true);
        this.invalidate();
        this.validate();
        this.repaint();

        final SwingWorker<Integer, Object> update = new SwingWorker<Integer, Object>()
        {
            @Override
            protected Integer doInBackground() throws IOException,
                    UpdateFailedException, InterruptedException,
                    MovieNotFoundException
            {
                database.update(progress);
                return 0;
            }

            @Override
            protected void done()
            {
                super.done();
                dispose();
                listener.done();
            }
        };

        try
        {
            update.execute();
        }
        catch (final Exception e)
        {
            new UpdateFailedDialog(database);
        }
    }

    /**
     * 
     * @author André Stannek
     */
    private static class UpdateFailedDialog extends JDialog
    {
        /**
         * generated UID.
         */
        private static final long serialVersionUID = -7818314192803414779L;

        /**
         * 
         * @param database
         *            the Database
         */
        public UpdateFailedDialog(final DB database)
        {
            super();
            this.setLayout(new GridLayout(1, 2));
            this.add(new JLabel(ResourceBundle.getBundle(
                    "de.imdb250junkie.locale.GUIStrings",
                    LocaleUtils.getLocale()).getString("update_failed")));
            final JButton button = new JButton(ResourceBundle.getBundle(
                    "de.imdb250junkie.locale.GUIStrings",
                    LocaleUtils.getLocale()).getString("ok"));
            button.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(final ActionEvent event)
                {
                    new MainWindow(database);
                    dispose();
                }
            });
            this.add(button);
        }
    }

}
